package com.yoomiito.app.pay

import cn.droidlover.xdroidmvp.net.ApiException
import cn.droidlover.xdroidmvp.net.ApiSubscriber
import cn.droidlover.xdroidmvp.net.BaseModel
import cn.droidlover.xdroidmvp.net.XApi
import com.yoomiito.app.R
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.order.OrderingInfo
import com.yoomiito.app.net.RequestParams
import com.yoomiito.app.net.YmApi
import com.yoomiito.app.ui.my.psw.SetPayPswActivity
import com.yoomiito.app.utils.L
import com.yoomiito.app.utils.ResUtil
import com.yoomiito.app.utils.UserInfoConfig
import com.yoomiito.app.utils.YMToast
import com.yoomiito.app.widget.PasswordView_1
import com.yoomiito.app.widget.TipDialog
import com.yoomiito.app.widget.dialog.PswInputDialog
import com.yoomiito.app.widget.dialog.TwoTypesOfPayDialog

class StarStorePayUtils(var context: BaseActivity<*>?, var payResultListener: (PayResult) -> Unit) {

    fun showPayDialog(orderingInfo: OrderingInfo) {
        val payDialog = TwoTypesOfPayDialog(context!!)
        payDialog.setMoney(orderingInfo)
        payDialog.payTypeChangeListener = {
            if (UserInfoConfig.havePayPsw()) {
                showPsw(orderingInfo, it)
            } else {
                showSetPayPswTip()
            }
            payDialog.dismiss()
        }
        payDialog.show()
    }

    /**
     * 提示设置密码框
     */
    private fun showSetPayPswTip() {
        val mTipDialog = TipDialog(context, TipDialog.TYPE_SELECT)
        mTipDialog?.setTopTipText(ResUtil.getString(R.string.my_tip))
        mTipDialog?.setTipText("为确保余额资金安全，请先设置千年艾支付密码")
        mTipDialog?.setSureText("马上设置")
        mTipDialog?.setcancleText("取消")
        mTipDialog?.setDialogCallback(object : TipDialog.Dialogcallback {
            override fun btnConfirm() {
                SetPayPswActivity.start(context)
            }

            override fun cancel(): Boolean = false

        })
        mTipDialog?.show()
    }

    private fun showPsw(orderingInfo: OrderingInfo, psyType: Int) {
        val mPswInputDialog = PswInputDialog(context!!)
        mPswInputDialog?.setTextChangedListener(object : PasswordView_1.TextChangedListener() {
            override fun textChanged(changeText: CharSequence) {

            }

            override fun textCompleted(text: CharSequence) {//余额支付
                context?.showLoading()
                mPswInputDialog.dismiss()
                pay(orderingInfo, text.toString(), psyType)
            }

            override fun forgetPsw() {
                mPswInputDialog?.dismiss()
            }
        })
        mPswInputDialog?.show()
    }

    private fun pay(orderingInfo: OrderingInfo, psd: String, payType: Int) {
        val params = hashMapOf("pay_type" to payType.toString(),
                                                     "pay_pwd" to psd,
                                                     "order_id" to orderingInfo.orderInfo.id)
        val param = RequestParams.encrypt(params)
        L.i("支付参数：${params.toString()} \n 加密后: $param")
        YmApi.getYmService()
                .starStoreToPay(param)
                .compose(context?.bindToLifecycle())
                .compose(XApi.getApiTransformerNoData<BaseModel<*>>())
                .compose(XApi.getScheduler())
                .subscribe(object : ApiSubscriber<BaseModel<*>>() {
                    override fun onSuccess(t: BaseModel<*>?) {
                        context?.dismissLoading()
                        payResultListener(PayResult(PayResult.PaySuccess))
                    }

                    override fun onFail(error: ApiException?) {
                        context?.dismissLoading()
                        YMToast.showText(error?.msg)
                    }

                })

    }

}